package com.nwpusp.transmitter.net;

import java.net.DatagramPacket;
import java.net.InetAddress;
import java.net.MulticastSocket;

import android.util.Log;

public class MultiSoc_Send_Thread extends Thread{
	
	private DatagramPacket outPacket = null;
	private MulticastSocket multiSoc;
	private boolean exit = false;
	private byte[]sendByte;
	
	public MultiSoc_Send_Thread(MulticastSocket multiSoc,InetAddress broadcastAddress,int MyIPAddress,String myName){
		byte nameByte [] = myName.getBytes();
		int length = 4+ nameByte.length;
		sendByte = new byte[length];
		sendByte[0] = (byte) (MyIPAddress & 0x0FF);
		sendByte[1] = (byte) ((MyIPAddress >> 8 ) & 0x0FF); 
		sendByte[2] = (byte) ((MyIPAddress >> 16 ) & 0x0FF);
		sendByte[3] = (byte) ((MyIPAddress >> 24 ) & 0x0FF); 
		for(int i=0;i<nameByte.length;i++){
			sendByte[4+i] = nameByte[i];
		}
		this.multiSoc = multiSoc;
		outPacket = new DatagramPacket(new byte[0] , 0 ,broadcastAddress , 8003);
	}
	
	public void run(){  
		while (!exit){  
			try {  
				outPacket.setData(sendByte);
	            multiSoc.send(outPacket);
	            sleep(100);
	         }catch (Exception e) {  
	        	 e.printStackTrace();  
	         }  
	     }   
	}
	
	public void setExit(boolean exit){
		this.exit = exit;
	}
}
